﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Anito.Data.Mapping;
using Anito.Data;

namespace Freetime.Base.Data
{
    [Serializable]
    public abstract class AuditableEntity : BaseEntity, IAuditable
    {
        protected int m_userCreated = default(int);
        protected int m_userModified = default(int);
        protected int m_userDeleted = default(int);

        protected bool m_isDeleted = false;
        protected DateTime m_dateCreated = default(DateTime);
        protected DateTime m_dateModified = default(DateTime);
        protected DateTime m_dateDeleted = default(DateTime);

        public AuditableEntity()
            : base()
        { }

        public AuditableEntity(ISession session)
            : base(session)
        { }

        [XmlElementAttribute("UserCreated")]
        [DataField(FieldName = "UserCreated")]
        public virtual int UserCreated
        {
            get
            {
                return m_userCreated;
            }
            set
            {
                SetField<int>("UserCreated", ref m_userCreated, value);
            }
        }

        [XmlElementAttribute("UserModified")]
        [DataField(FieldName = "UserModified")]
        public virtual int UserModified
        {
            get
            {
                return m_userModified;
            }
            set
            {
                SetField<int>("UserModified", ref m_userModified, value);
            }
        }

        [XmlElementAttribute("UserDeleted")]
        [DataField(FieldName = "UserDeleted")]
        public virtual int UserDeleted
        {
            get
            {
                return m_userDeleted;
            }
            set
            {
                SetField<int>("UserDeleted", ref m_userDeleted, value);
            }
        }

        [XmlElementAttribute("DateCreated")]
        [DataField(FieldName = "DateCreated")]
        public virtual DateTime DateCreated
        {
            get
            {
                return m_dateCreated;
            }
            set
            {
                SetField<DateTime>("DateCreated", ref m_dateCreated, value);
            }
        }

        [XmlElementAttribute("DateModified")]
        [DataField(FieldName = "DateModified")]
        public virtual DateTime DateModified
        {
            get
            {
                return m_dateModified;
            }
            set
            {
                SetField<DateTime>("DateModified", ref m_dateModified, value);
            }
        }

        [XmlElementAttribute("DateDeleted")]
        [DataField(FieldName = "DateDeleted")]
        public virtual DateTime DateDeleted
        {
            get
            {
                return m_dateDeleted;
            }
            set
            {
                SetField<DateTime>("DateDeleted", ref m_dateDeleted, value);
            }
        }

        [XmlElementAttribute("IsDeleted")]
        [DataField(FieldName = "IsDeleted")]
        public virtual bool IsDeleted
        {
            get
            {
                return m_isDeleted;
            }
            set
            {
                SetField<bool>("IsDeleted", ref m_isDeleted, value);
            }
        }

        public override void Save()
        {
            if ((this as IDataObject).GetObjectState() == DataObjectState.Modified)
                m_dateModified = DateTime.Now;
            else if ((this as IDataObject).GetObjectState() == DataObjectState.New)
                m_dateCreated = DateTime.Now;
            else if ((this as IDataObject).GetObjectState() == DataObjectState.Deleted)
                m_dateDeleted = DateTime.Now;
            base.Save();
        }  
    }
}
